package com.example.himalayaproject.find.anchor.adaptor;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.activity.OwnerLoginActivity;
import com.example.himalayaproject.find.anchor.bean.Iterm_entity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * Created by 李纪龙 on 2016/12/2.
 * 广播中recyclerView 的适配器
 */
public class AnchorFragment_Adaptor extends RecyclerView.Adapter<AnchorFragment_Adaptor.MyHolder> implements View.OnClickListener {
    private Context context;
    private List<Iterm_entity> data;
    private IsNextPageListener listener;

    public AnchorFragment_Adaptor(Context context, IsNextPageListener listener) {
        this.context = context;
        data = new ArrayList<>();
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, OwnerLoginActivity.class);
        context.startActivity(intent);
    }


    public static class MyHolder extends RecyclerView.ViewHolder {

        private TextView titleText, layoutText1, layoutText2, layoutText3;
        private ImageView imageView1, imageView2, imageView3;
        private ImageButton imageButton1, imageButton2, imageButton3;

        public MyHolder(View itemView) {
            super(itemView);
            Log.d("1621", "context");
            titleText = (TextView) itemView.findViewById(R.id.anchor_item_title_text);
            layoutText1 = (TextView) itemView.findViewById(R.id.anchor_layout1_text1);
            layoutText2 = (TextView) itemView.findViewById(R.id.anchor_layout1_text2);
            layoutText3 = (TextView) itemView.findViewById(R.id.anchor_layout1_text3);
            imageView1 = (ImageView) itemView.findViewById(R.id.anchor_layout1_ima1);
            imageView2 = (ImageView) itemView.findViewById(R.id.anchor_layout1_ima2);
            imageView3 = (ImageView) itemView.findViewById(R.id.anchor_layout1_ima3);
            imageButton1 = (ImageButton) itemView.findViewById(R.id.image_button_1);
            imageButton2 = (ImageButton) itemView.findViewById(R.id.image_button_2);
            imageButton3 = (ImageButton) itemView.findViewById(R.id.image_button_3);

        }

    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.anchorfragment_iterm, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        Iterm_entity iterm = data.get(position);
        holder.titleText.setText(data.get(position).getTitle());
        if (iterm.getList() != null) {
            holder.layoutText1.setText(data.get(position).getList().get(0).getNickname());
            holder.layoutText2.setText(data.get(position).getList().get(1).getNickname());
            holder.layoutText3.setText(data.get(position).getList().get(2).getNickname());
            holder.imageButton1.setOnClickListener(this);
            holder.imageButton2.setOnClickListener(this);
            holder.imageButton3.setOnClickListener(this);
            Picasso.with(context).load(data.get(position).getList().get(0).getSmallLogo()).into(holder.imageView1);
            Picasso.with(context).load(data.get(position).getList().get(1).getSmallLogo()).into(holder.imageView2);
            Picasso.with(context).load(data.get(position).getList().get(2).getSmallLogo()).into(holder.imageView3);
        }
        if (position == data.size() - 1) {
            listener.isNextPage();
        }
    }

    public void addAll(Collection<? extends Iterm_entity> collection) {
        int size = data.size();
        data.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface IsNextPageListener {
        void isNextPage();
    }

}
